package cn.tanghom.app.oa.controller;

import static cn.tanghom.support.web.BaseController.THEME_PATH;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.flowable.common.engine.api.FlowableException;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.tanghom.app.oa.model.Candidate;
import cn.tanghom.app.oa.model.Position;
import cn.tanghom.app.oa.model.Talent;
import cn.tanghom.app.oa.repository.CandidateRepository;
import cn.tanghom.app.oa.repository.EduExpRepository;
import cn.tanghom.app.oa.repository.TalentRepository;
import cn.tanghom.app.oa.repository.WorkExpRepository;
import cn.tanghom.app.oa.service.RecruitmentProcessService;
import cn.tanghom.app.oa.service.ResumeService;
import cn.tanghom.app.oa.service.impl.ResumeServiceWarper;
import cn.tanghom.app.system.model.User;
import cn.tanghom.support.utils.CommonUtils;
import cn.tanghom.support.web.ContextHolder;



/**
 * 候选人管理
 * @author Hunteron-cp
 *
 */
@Controller
@RequestMapping("/oa/candidate")
public class CandidateController {	
	@Autowired
	protected RuntimeService runtimeService;
	
	@Autowired
	protected ResumeServiceWarper resumeService;	
	
	
	@Resource
    private RecruitmentProcessService matchService;    	
	
	@Resource
    private CandidateRepository candidateRepository;

    
    @ResponseBody
    @RequestMapping(value = "/t/{talentId}")
    public Talent matchTalent(@PathVariable Long talentId) {
    	Talent result = resumeService.getResume("",talentId);
        return result;
    }
    

    //对单个职位和单个简历，启动职位招聘流程:MatchProcess
    @RequestMapping(value = "/match/{id}")
    @ResponseBody
    public String start(@PathVariable long id,long talentId) {
        try {
            User loger = ContextHolder.getUser();
            Position position = matchService.findById(id);
            position.setUserId(loger.getId());
            position.setUpdateTime(new Date());
            Map<String, Object> variables = new HashMap<String, Object>();
            variables.put("talentId", talentId);
            variables.put("positionId", id);
            ProcessInstance processInstance = matchService.startWorkflow(position, variables);
            
            return CommonUtils.msgCallback(true, "流程已启动", "", processInstance.getId());
        } catch (FlowableException e) {
            if (e.getMessage().indexOf("no processes deployed with key") != -1) {
                return CommonUtils.msgCallback(false, "系统内部错误，请联系管理员（没有部署流程，请在[工作流]->[流程管理]页面点击<重新部署流程>），" + e.getMessage(), "", null);
            } else {
                return CommonUtils.msgCallback(false, "系统内部错误,请联系管理员（启动请假流程失败），" + e.getMessage(), "", null);
            }
        } catch (Exception e) {
            return CommonUtils.msgCallback(false, "系统内部错误,请联系管理员，" + e.getMessage(), "", null);
        }
    }
    
   

    //收到推荐对，这里人才和职位必须是匹配的，并保存该人职对
    @RequestMapping(value = "/receive", method = RequestMethod.POST)
    @ResponseBody
    public String receive(Long positionId,Long talentId) {
        try {  
    	    Candidate cand = new Candidate();
    	    cand.setPositionId(positionId);
    	    cand.setTalentId(talentId);
    	    cand.setCreateTime(new Date());
    	    cand.setMatchScore(1.0f);
    	    
    	    candidateRepository.save(cand);
            return CommonUtils.msgCallback(true, "Candidate已创建", "", "talentId:"+talentId);
        
        } catch (Exception e) {
            return CommonUtils.msgCallback(false, "系统内部错误,请联系管理员，" + e.getMessage(), "", null);
        }
    }    
}
